package com.qishon.apiTest;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * @Description 生成指定长度指定复杂度的字符串
 * @Author 		sangdi.he
 * @Time 		2016-07-20 12:02
 */
public class GenerateRandom {
	
	private String number = "0123456789";
	private String lower_case = "abcdefghijklmnopqrstuvwxyz";
	private String upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private String alpabet;
	
	/**
	 * @Description		生成随机字符串，根据可选的复杂度和长度来生成
	 * @param chose 	可选复杂度：1，纯数字；2，数字加小写字母；3，数字加大写字母；4，数字加小写加大写字母
	 * @param length 	想要的字符串长度，这里可能不能生成超过String类型能够容纳长度的字符串
	 * @return 			返回随机生成的字符串
	 * @Author 			sangdi.he
	 * @Time 			2016-07-20 12:02
	 * @Update_History 	无
	 */
	public String gen(int chose, int length) {
		switch (chose) {
		case 1:
			alpabet = number;
			break;
		case 2:
			alpabet = number + lower_case;
			break;
		case 3:
			alpabet = number + upper_case;
			break;
		case 4:
			alpabet = number + lower_case + upper_case;
			break;
		default:
			alpabet =  number + lower_case + upper_case;
			break;
		}
		
		int n = alpabet.length();
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		
		for (int i=0; i<length; i++) {
			int randomIndex = random.nextInt(n);
			sb.append(alpabet.charAt(randomIndex));
		}
		
		return sb.toString();
	}
	
	public static void main(String[] args) {
		GenerateRandom gr = new GenerateRandom();
		PrintWriter out;
		try {
			out = new PrintWriter(new File("./1.txt"));
			out.print(gr.gen(4, 4001));
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}

	}
}
